package kr.or.javacafe.board.bo;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import kr.or.javacafe.board.dao.BoardDAO;
import kr.or.javacafe.board.domain.*;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Qualifier;

@Service
public class BoardBOImpl implements BoardBO {

	@Autowired
	private BoardDAO boardDAO;



	@Override
	@Transactional(readOnly=true)
	public List<Comment> listComent(Comment comment) {
		return boardDAO.selectCommentList(comment);
	}
	
	
	@Override
	@Transactional(readOnly=true)
	public ArticleFile getDownloadFile(int fileSeq) {
		ArticleFile articleFile = new ArticleFile();
		articleFile.setFileSeq(fileSeq);
		
		return boardDAO.selectDownloadFile(articleFile);
	}
	
	
	@Override
	@Transactional
	public void addComment(Comment comment) {
		String strId;
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		strId = ((UserDetails)principal).getUsername();

		Date now = new Date();
		
		comment.setRegrEmpNo(strId);
		comment.setRegYmdt(now);
		
		boardDAO.insertComment(comment);
	}
	
	@Override
	@Transactional
	public void removeComment(Comment comment) {
		boardDAO.deleteComment(comment);
	}
	
	
	@Override
	@Transactional
	public void removeArticleFile(ArticleFile file) {
		boardDAO.deleteArticleFile(file);
	}
	
	
	@Override
	@Transactional(readOnly=true)
	public List<Article> getPreviewNoticeArticle() {
		return boardDAO.selectPreviewNoticeArticle();
	}
	
	
	@Override
	@Transactional(readOnly=true)
	public List<Article> getPreviewRecentArticle() {
		return boardDAO.selectPriviewRecentArticle();
	}

	
	@Override
	@Transactional
	public void addArticle(Article article, List<ArticleFile> objFileList) {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String strId = ((UserDetails)principal).getUsername();

		Date now = new Date();
		int intNewAtcNo = boardDAO.selectArticleNoSeq();
		
		if (article.getAtcNo() == 0) {
			// 신규 등록
			int intNewBbsThreadNo = boardDAO.selectBbsThreadNo(article.getBbsId());
			
			article.setAtcNo(intNewAtcNo);
			article.setBbsThread(intNewBbsThreadNo);
			article.setBbsDepth(0);
			article.setReadCnt(0);
			article.setRegrEmpNo(strId);
			article.setRegYmdt(now);
			article.setModrEmpNo(strId);
			article.setModYmdt(now);			
		} else {
			// 답글 등록
			int intParentThreadNo = article.getBbsThread();
			int intPrevThreadNo = (intParentThreadNo-1)/1000*1000;
			int intDepth = article.getBbsDepth() + 1;
			
			Article paramAtc = new Article();
			paramAtc.setBbsId(article.getBbsId());
			paramAtc.setStartBbsThread(intParentThreadNo);
			paramAtc.setEndBbsThread(intPrevThreadNo);
			boardDAO.updateReplyArticle(paramAtc);
			
			article.setAtcNo(intNewAtcNo);
			article.setBbsThread(intParentThreadNo - 1);
			article.setBbsDepth(intDepth);
			article.setReadCnt(0);
			article.setRegrEmpNo(strId);
			article.setRegYmdt(now);
			article.setModrEmpNo(strId);
			article.setModYmdt(now);
		}
		boardDAO.insertArticle(article);
		
		for (ArticleFile aFile : objFileList) {
			aFile.setAtcNo(intNewAtcNo);		
			boardDAO.insertArticleFile(aFile);
		}
	}

	
	@Override
	@Transactional
	public void editArticle(Article article, List<ArticleFile> objFileList) {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String strId = ((UserDetails)principal).getUsername();

		Date now = new Date();
		
		article.setReadCnt(0);
		article.setModrEmpNo(strId);
		article.setModYmdt(now);				
		boardDAO.updateArticle(article);
		
		for (ArticleFile aFile : objFileList) {
			aFile.setAtcNo(article.getAtcNo());		
			boardDAO.insertArticleFile(aFile);
		}
	}

	
	@Override
	@Transactional
	public void removeArticle(Article article, ArticleFile file) {
		boardDAO.deleteArticleFile(file);
		boardDAO.deleteArticle(article);
	}

	
	@Override
	@Transactional(readOnly=true)
	public Bbs getBbsInfo(String bbsId) {
		Bbs bbs = new Bbs();
		bbs.setBbsId(bbsId);
		
		return boardDAO.selectBbs(bbs);
	}
	
	
	@Override
	@Transactional(readOnly=true)
	public List<Article> listArticle(Article article) {
		return boardDAO.selectArticleList(article);
	}

	@Override
	@Transactional(readOnly=true)
	public int getListArticleCount(Article article) {
		return boardDAO.selectArticleListCount(article);
	}

	@Override
	@Transactional
	public Article viewArticle(int atcNo) {
		Article article = new Article();
		article.setAtcNo(atcNo);
		article.setReadCnt(1);
		boardDAO.updateArticle(article);
		
		return boardDAO.selectArticle(atcNo);
	}
	
	@Override
	@Transactional
	public void hitArticle(int atcNo) {
		Article article = new Article();
		article.setAtcNo(atcNo);
		article.setReadCnt(1);
		//boardDAO.updateArticle(article);
	}

	
}






